﻿var lastKWH = 0;
var systemTime = undefined;
var endTime = undefined;
var demo = undefined;
var options = undefined;
var ip = undefined;
var lastSearchTime = undefined;
var searchTimer;
//查询
$(document).ready(function() {
    getIP();
    setInterval(upgrade, 5 * 1000);
});
function upgrade() {
	window.control.upgrade();
}
function getIP(){
	// ip = window.control.readFiles();
	$("#versionName").html("当前版本号:v1.0.2(20180118)");
    time();
    setInterval(setTime, 1000);
    search();
}
function formatTime(time) {
    var second = parseInt(time, 10); // 秒
    var minute  = 0; // 分
    var hour = 0; // 时
    if(second > 60) {
    	minute = parseInt(second / 60, 10);
    	second = second % 60;
        if(minute > 60) {
        	hour = parseInt(minute / 60, 10);
        	minute = minute % 60;
        	return hour + "小时" + minute + "分" + second + "秒";
        } else {
        	return minute + "分" + second + "秒";
        }
    } else {
    	return second + "秒";
    }
}

function timeStamp2String(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}

function setTime() {
    $("#time").html(formatTime1(systemTime));
    if (systemTime) {
		systemTime = systemTime + 1000;
	} else {
		time();
	}
	if(systemTime && systemTime > lastSearchTime + 30000){
		lastSearchTime = systemTime;
		clearTimeout(searchTimer);
		search();
		$("#alert").attr("hidden", false);
	}
}

function time() {
    $.ajax({
        type : "post",
        async:false,
        cache : false,
        url : "http://" + ip + "/systemTime",
        dataType : "jsonp",
        jsonpCallback:"successTime",
        timeout : 30000,
        scriptCharset: 'utf-8',
        success: function(data) {
            systemTime = data;
            lastSearchTime = systemTime;
            setTime();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        },
        timeout: function(data){
        }
    })
}

function search(){
    var sumTotalChargingKWH = 0;
    var sumTotalChargingDTTM = 0;
    var carCount = [];
    var appInfo = undefined;
	$.ajax({
	    type : "post",
  	    async: false,
  	    cache : false,
  	    url : "http://" + ip + "/andRecord",
  	    dataType : "jsonp",
  	    jsonpCallback:"successCallback",
  	    timeout : 30000,
		scriptCharset: 'utf-8',
		success: function(data){
	        appInfo = data.appInfo;
	        lastSearchTime = systemTime;
	        $("#alert").attr("hidden", true);
            $("#abnormalCnt").html(appInfo[0].abnormalCnt);
            $("#signCnt").html(appInfo[0].signCnt);
//            if(appInfo[0].dailyCnt == 1){
//                $("#position003").html('<img src="../static/images/tou_duihao.png" height="30" width="33"/>');
//            }
//            if(appInfo[0].meterCnt == 1){
//                $("#position002").html('<img src="../static/images/tou_duihao.png" height="30" width="33"/>');
//            }
//            if(appInfo[0].dutyCnt == 1){
//                $("#position004").html('<img src="../static/images/tou_duihao.png" height="30" width="33"/>');
//            }
		    //清除页面信息
			clearTable("chargingRecordTbody");
			var todayRecord = data.todayRecord;
			var todayStatistics = data.todayStatistics[0];
			var length = todayRecord.length;
			if (todayRecord[0] == null){
                $("#hour").html("0");
                $("#minute").html("0");
                $("#second").html("0");
                $("#sminute").html("0");
                $("#shour").html("0");
                $("#ssecond").html("0");
                $("#carCount").html("0");
                $("#chargingLength").html("0");
                $("#totalCharging").html("0");
                $("#maxKWH").html("0");
                $("#reissue").html("0");
			}else {
				var maxKWH = todayStatistics.totalChargingKWH;
				var maxDTTM = todayStatistics.totalChargingDTTM;
				var reissue = todayStatistics.resendCount;
				var carCount = todayStatistics.carCount;
				var sumTotalChargingKWH = todayStatistics.sumTotalChargingKWH;
				var sumTotalChargingDTTM = todayStatistics.sumTotalChargingDTTM;
				var totalChargingCount = todayStatistics.chargingCount;
				if(!options){
	                options = {
	                    useEasing : true,
	                    useGrouping : true,
	                    separator : ',',
	                    decimal : '.',
	                    prefix : '',
	                    suffix : ''
	                };
                }
                if (sumTotalChargingKWH != lastKWH) {
	                demo = new CountUp("totalCharging", lastKWH, sumTotalChargingKWH, 0, 2, options);
	                demo.start();
	                lastKWH = sumTotalChargingKWH;
	                demo = null;
                }
                var second = parseInt(sumTotalChargingDTTM, 10); // 秒
                var minute  = 0; // 分
                var hour = 0; // 时
                if(second > 60) {
                    minute = parseInt(second / 60, 10);
                    second = second % 60;
                    if(minute > 60) {
                        hour = parseInt(minute / 60, 10);
                        minute = minute % 60;
                    }
                }
                var ssecond = parseInt(maxDTTM, 10); // 秒
                var sminute  = 0; // 分
                var shour = 0; // 时
                if(ssecond > 60) {
                    sminute = parseInt(ssecond / 60, 10);
                    ssecond = ssecond % 60;
                    if (sminute > 60) {
                        shour = parseInt(sminute / 60, 10);
                        sminute = sminute % 60;
                    }
                }
                $("#hour").html(hour);
                $("#minute").html(minute);
                $("#second").html(second);
                $("#shour").html(shour);
                $("#sminute").html(sminute);
                $("#ssecond").html(ssecond);
				$("#carCount").html(carCount);
				$("#chargingLength").html(totalChargingCount);
				$("#maxKWH").html(maxKWH);
				$("#reissue").html(reissue);
			}
			if (length == 0) {
			} else {
				var sum = 0;
				for(var i=0; i<length; i++){
					var tbody2 = $("#chargingRecordTbody");
					var $tr = $('<tr>').appendTo(tbody2);
					endTime = todayRecord[i].endChargingDTTM;
					if (endTime >= systemTime - 8*1000) {
                        $("<td>").addClass("blink").html(todayRecord[i].equipmentName).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].carNumber).appendTo($tr);
                        $("<td>").addClass("blink").html(timeStamp2String(todayRecord[i].startChargingDTTM)).appendTo($tr);
                        $("<td>").addClass("blink").html(timeStamp2String(endTime)).appendTo($tr);
                        $("<td>").addClass("blink").html(formatTime(todayRecord[i].totalChargingDTTM)).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].startAmmeterNo).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].endAmmeterNo).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].totalChargingKWH).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].startSOC).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].eNDSOC).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].totalSOC).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].endReason).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].cardNo).appendTo($tr);
                        $("<td>").addClass("blink").html(todayRecord[i].lineName).appendTo($tr);
                    } else{
                        $("<td>").addClass("white").html(todayRecord[i].equipmentName).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].carNumber).appendTo($tr);
                        $("<td>").addClass("white").html(timeStamp2String(todayRecord[i].startChargingDTTM)).appendTo($tr);
                        $("<td>").addClass("white").html(timeStamp2String(todayRecord[i].endChargingDTTM)).appendTo($tr);
                        $("<td>").addClass("white").html(formatTime(todayRecord[i].totalChargingDTTM)).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].startAmmeterNo).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].endAmmeterNo).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].totalChargingKWH).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].startSOC).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].eNDSOC).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].totalSOC).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].endReason).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].cardNo).appendTo($tr);
                        $("<td>").addClass("white").html(todayRecord[i].lineName).appendTo($tr);
                    }
				}
			}
			searchTimer = setTimeout("search()", 5000);
		},
		error: function(data){
			if (data.status == "403" && data.statusText == "Forbidden") {
			} else {
			}
			searchTimer = setTimeout("search()", 30000);
		},
		timeout: function(data){
			searchTimer = setTimeout("search()", 30000);
		}
	});
}
